package com.crazyhat.crazyhat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crazyhat.crazyhat.entity.Club;
import com.crazyhat.crazyhat.pojo.ClubCategory;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.service.IClubService;



/**
 * 主页控制器
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IClubService clubService;
    @Autowired
    private ClubCategory clubCategory;


    /**
     * 搜索社团
     * @param keyword 搜索关键字
     * @return 匹配的社团列表
     */
    @GetMapping("/search")
    public Result<?> searchClubs(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) List<String> categories,
            @RequestParam(required = false, defaultValue = "false") boolean forceNormalResult) {
        // Trim the keyword to remove leading and trailing spaces
        if (keyword != null) {
            keyword = keyword.trim();
        }

        if ((keyword == null || keyword.isEmpty()) && (categories == null || categories.isEmpty())) {
            return Result.error("关键字和类别不能均为空");
        }

        // Check if the keyword is "喻子豪" and forceNormalResult is false, return a special response with an image
        if ("喻子豪".equals(keyword) && !forceNormalResult) {
            return Result.success(new SpecialResponse("https://pic.imgdb.cn/item/66988b7fd9c307b7e92cae71.jpg"));
        }

        List<Club> clubs = clubService.searchClubs(keyword, categories);
        return Result.success(clubs);
    }

    // Define a class to encapsulate the special response
    public static class SpecialResponse {
        private String imageUrl;

        public SpecialResponse(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }
    }



    /**
     * 获取热门社团
     * @param limit 返回的社团数量
     * @return 热门社团列表
     */
    @GetMapping("/popular")
    public Result getTopClubs() {
        List<Club> clubs = clubService.getTopClubsByPopularity(10);
        return Result.success(clubs);
    }

    /**
     * 获取最新发布社团
     * @param limit 返回的社团数量
     * @return 最新发布社团列表
     */
    @GetMapping("/latestClubs")
    public Result getLatestClubs() {
        List<Club> clubs = clubService.getLatestClubs(10);
        return Result.success(clubs);
    }

    /**
     * 获取社团分类
     * @return 社团分类
     */
    @GetMapping("/clubCategory")
    public Result getClubCategory() {
        List<String> categories = clubCategory.getClubCategoryList();
        return Result.success(categories);
    }
}
